package com.ruoyi.common.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.ExecutionException;

/**
 * 短信发送工具类
 */
public class SMSUtils {

    private static StaticCredentialProvider provider;
    // 阿里云ACCESS_KEY
    private static final String ACCESS_KEY = "LTAI5t5ivXD25LDNpN7jaE6C";
    // ACCESS_KEY_SECRET
    private static final String SECRET_KEY = "wCkCXWL0yBOqNJBHcOnCwjFrnUQ3zq";

    static {
        provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ACCESS_KEY)
                .accessKeySecret(SECRET_KEY)
                .build());
    }

    /**
     * 发送短信验证码
     * @param phone
     * @param code
     */
    public static void sendSMS(String phone,String code) throws ExecutionException, InterruptedException {
        AsyncClient client = AsyncClient.builder()
                .region("cn-qingdao") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("重庆小猿科技") // 签名
                .templateCode("SMS_191803038") // 模板
                .phoneNumbers(phone) // 手机号
                .templateParam("{\"code\":\""+code+"\"}") // 模板参数
                .build();
        client.sendSms(sendSmsRequest);
        client.close();
    }
}
